<?php 
# ------------------------------------------------------|
# @author  : Alankar More                               |
# @purpose : When user want to see his  bookings        |
# ------------------------------------------------------|
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/users.php';
require_once '../../model/reviews.php';
/***************************************/
## Creating required objects 
$userObj = new Model_Users();
$reviewObj = new Model_Reviews();
//-----------------------------//
## Checking user is signin or not if not then redirecting to the signin page 
if($_SESSION['getbids_id']==''){
	$_SESSION['pageback'] = "jobs/";
	header("location:".SITE_URL."/signin/");
	exit;
}else{
	$userId = $_SESSION['getbids_id'];
}

if($_GET['id1']!='' && $_GET['id2']!=''){	
	
	if($_GET['id2'] == 'post'){
		$jobId = $_GET['id1'];	
		$job_details = $userObj->getUserViewjobDetailsByJIDANDSTATUS($jobId,'Completed');
		$user_details = $userObj->getUserDetailsByUserId($job_details['userid']);
		$job_details['username'] = $user_details['first_name']." ".$user_details['last_name'];
		
		#echo "<pre>";print_r($job_details);exit;
		$smarty->assign('job_details', $job_details );
	}

	if($_GET['id2'] == 'see'){
		$reviewId = $_GET['id1'];
		$review_details = $reviewObj->getReviewById($reviewId);
		//echo "<pre>";print_r($review_details );exit;	 
		$total_rating = $review_details['rating'];
		$smarty->assign('total_rating', $total_rating);
		if($total_rating < 5){
			$remaining_rating = 5 - $total_rating;
			$smarty->assign('remaining_rating', $remaining_rating);
		} 
		$smarty->assign('review_details', $review_details);
	}

}		

if(!empty($_POST)){
	if(isset($_POST['post_msg'])){
		
		$ratingArray = array();
		$ratingArray['rating']   = $_POST['txt_rating'];
		$ratingArray['message']  = trim(addslashes(strip_tags($_POST['send_message'])));		
		$ratingArray['from_id']  = $userId;
		$ratingArray['to_id'] 	 = $job_details['userid'];
		$ratingArray['job_id'] = $job_details['jobid'];
		$ratingArray['added_date'] = getCurDate();
		
		//echo "<pre>";print_r($ratingArray);exit;
		$reviewObj->addReview($ratingArray);
		
		// update review count
		$user_details = $userObj->getUserDetailsByUserId($job_details['userid']);
		$edituserArray = array();
		$count = $user_details['review_count'] + 1;
		$edituserArray['review_count'] = $count;		
		$userObj->editUserValueById($edituserArray,$job_details['userid']);

		
		
		$_SESSION['msg'] = '<div class="success_msg" style="margin:5px;"><span>Your review has been posted successfully!</span></div>';
?>
          <script type="text/javascript">
			SITEROOT ="<?php echo SITE_URL; ?>"; 
			var redirectPage = SITEROOT + "/jobs/myjobs/";	
			window.parent.location.href = redirectPage;
	       </script>
<?
	exit;
	}
}		

if($_SESSION['msg']!=''){
	$smarty->assign('msg',$_SESSION['msg']);
	unset($_SESSION['msg']);
}
## redirecting to the template 
$smarty->display(TEMPLATEDIR."/modules/jobs/postreview.tpl");

## Unsetting all used objects
unset($_SESSION['msg']);
unset($userObj);
unset($smarty);
?>